package com.ymatou.iapi.sellercs.testcase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.sellercs.parameter.GetHistorySessionListBean;
import com.ymatou.iapi.sellercs.parameter.HistorySessionDto;
import com.ymatou.iapi.sellercs.parameter.SendMessageBean;
import com.ymatou.iapi.sellercs.service.GetHistorySessionListCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.business.service.SellerCSIMCallService;
import com.ymttest.database.model.YmtCssession;
import com.ymttest.database.sqlwapper.YmtSellerCSIMWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
/**
*获取用户历史会话列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetHistorySessionList{
	private static GetHistorySessionListBean gethistorysessionlistBean;
	private static GetHistorySessionListCall gethistorysessionlistCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	private static String sellername = EnvSetup.getData("autotest_sellerName1");
	private static int zsellerId = Integer.parseInt(EnvSetup.getData("autotest_seller_z"));
	private static YmtSellerCSIMWapper YmtSellerCSIMWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取用户历史会话列表");
	}

	@Before
	public void caseUp() throws ClientProtocolException, IOException {
		gethistorysessionlistBean=new GetHistorySessionListBean();
		gethistorysessionlistCall=new GetHistorySessionListCall();
		YmtSellerCSIMWapper=new YmtSellerCSIMWapper();
		SellerCSIMCallService.OffLine(sellerId, null);
		SellerCSIMCallService.OffLine(zsellerId, null);
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(gethistorysessionlistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetHistorySessionList_001() {
		Logger.start(true,"获取用户历史会话列表,成功");
		try {
			Logger.comment("注册新用户");
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
			Logger.comment("删除旧数据");
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(sellerId);
			// APP上线
			SellerCSIMCallService.AppLogin(sellerId);
			SendMessageBean sendmessageBean = SellerCSIMCallService.initSendmessageBean(userId, sellerId, "connect");
			SellerCSIMCallService.SendMessage(sendmessageBean);
			SellerCSIMCallService.Assign();
			Thread.sleep(2000);
			SellerCSIMCallService.OffLine(sellerId, null);
			Thread.sleep(2000);
			List<YmtCssession> cinfos = YmtSellerCSIMWapper.selectYmtCssession(sendmessageBean.getToUserId(), sendmessageBean.getFromUserId());
			SellerCSIMCallService.MoveClosesessionToHistorySessionTable();
			Thread.sleep(2000);
			
			gethistorysessionlistBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(1, "yyyy/MM/dd HH:mm:ss"));
			gethistorysessionlistBean.setMasterCsId(sellerId);
			gethistorysessionlistBean.setPageSize(20);
			gethistorysessionlistBean.setAppId("auto");
			gethistorysessionlistCall.setData(gethistorysessionlistBean);
			gethistorysessionlistCall.callService();			
			Logger.verifyEquals("200", gethistorysessionlistCall.getString("Status"), "验证Status");
			Logger.comment("验证第一条记录");
			ArrayList<HistorySessionDto> historycinfos = gethistorysessionlistCall.getHistorySessionListResModels();
//			Logger.verifyEquals(gethistorysessionlistBean.getPageSize(), historycinfos.size(), "验证分页条数");
			Logger.verifyEquals(cinfos.get(0).getCloseType(),historycinfos.get(0).getCloseType(), "验证CloseType");
			Logger.verifyEquals(cinfos.get(0).getBeginTime(),historycinfos.get(0).getBeginTime(), "验证getBeginTime");
			Logger.verifyEquals(cinfos.get(0).getCSId(),historycinfos.get(0).getCSId(), "验证getCSId");
			Logger.verifyEquals(cinfos.get(0).getEndTime(),historycinfos.get(0).getEndTime(), "验证endtime");
			Logger.verifyEquals(cinfos.get(0).getFromPlatform(),historycinfos.get(0).getFromPlatform(), "验证getFromPlatform");
			Logger.verifyEquals(cinfos.get(0).getLaunchTime(),historycinfos.get(0).getLaunchTime(), "验证getLaunchTime");
			Logger.verifyEquals(cinfos.get(0).getMark(),historycinfos.get(0).getMark(), "验证getMark");
			Logger.verifyEquals(cinfos.get(0).getOpenType(),historycinfos.get(0).getOpenType(), "验证getOpenType");
			Logger.verifyEquals(cinfos.get(0).getSessionNumber(),historycinfos.get(0).getSessionNumber(), "验证getSessionNumber");
			Logger.verifyEquals(cinfos.get(0).getStatus(),historycinfos.get(0).getStatus(), "验证getStatus");
			Logger.verifyEquals(cinfos.get(0).getUserId(),historycinfos.get(0).getUserId(), "验证getUserId");
			Logger.verifyEquals(cinfos.get(0).getUserName(),historycinfos.get(0).getUserName(), "验证getUserName");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetHistorySessionList_002() {
		Logger.start(true,"不输入MasterCsId失败");
		try {
			gethistorysessionlistBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(1, "yyyy/MM/dd HH:mm:ss"));
//			gethistorysessionlistBean.setMasterCsId(sellerId);
			gethistorysessionlistBean.setPageSize(20);
			gethistorysessionlistBean.setAppId("auto");
			gethistorysessionlistCall.setData(gethistorysessionlistBean);
			gethistorysessionlistCall.callService();			
			Logger.verifyEquals("400", gethistorysessionlistCall.getString("Status"), "验证Status");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetHistorySessionList_003() {
		Logger.start(true,"UserNameKeyWord过滤成功");
		try {
			Logger.comment("注册新用户");
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
			Logger.comment("删除旧数据");
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(sellerId);
			// APP上线
			SellerCSIMCallService.AppLogin(sellerId);
			SendMessageBean sendmessageBean = SellerCSIMCallService.initSendmessageBean(userId, sellerId, "connect");
			SellerCSIMCallService.SendMessage(sendmessageBean);
			SellerCSIMCallService.Assign();
			Thread.sleep(2000);
			SellerCSIMCallService.OffLine(sellerId, null);
			Thread.sleep(2000);
			List<YmtCssession> cinfos = YmtSellerCSIMWapper.selectYmtCssession(sendmessageBean.getToUserId(), sendmessageBean.getFromUserId());
			SellerCSIMCallService.MoveClosesessionToHistorySessionTable();
			Thread.sleep(2000);
			
			gethistorysessionlistBean.setUserNameKeyWord(regbean.getLoginId());
			gethistorysessionlistBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(1, "yyyy/MM/dd HH:mm:ss"));
			gethistorysessionlistBean.setMasterCsId(sellerId);
			gethistorysessionlistBean.setPageSize(20);
			gethistorysessionlistBean.setAppId("auto");
			gethistorysessionlistCall.setData(gethistorysessionlistBean);
			gethistorysessionlistCall.callService();			
			Logger.verifyEquals("200", gethistorysessionlistCall.getString("Status"), "验证Status");
			
			Logger.comment("验证第一条记录");
			ArrayList<HistorySessionDto> historycinfos = gethistorysessionlistCall.getHistorySessionListResModels();
			Logger.verifyEquals(1, historycinfos.size(), "过滤条件成功-只有这条记录被滤出");
			Logger.verifyEquals(cinfos.get(0).getCloseType(),historycinfos.get(0).getCloseType(), "验证CloseType");
			Logger.verifyEquals(cinfos.get(0).getBeginTime(),historycinfos.get(0).getBeginTime(), "验证getBeginTime");
			Logger.verifyEquals(cinfos.get(0).getCSId(),historycinfos.get(0).getCSId(), "验证getCSId");
			Logger.verifyEquals(cinfos.get(0).getEndTime(),historycinfos.get(0).getEndTime(), "验证endtime");
			Logger.verifyEquals(cinfos.get(0).getFromPlatform(),historycinfos.get(0).getFromPlatform(), "验证getFromPlatform");
			Logger.verifyEquals(cinfos.get(0).getLaunchTime(),historycinfos.get(0).getLaunchTime(), "验证getLaunchTime");
			Logger.verifyEquals(cinfos.get(0).getMark(),historycinfos.get(0).getMark(), "验证getMark");
			Logger.verifyEquals(cinfos.get(0).getOpenType(),historycinfos.get(0).getOpenType(), "验证getOpenType");
			Logger.verifyEquals(cinfos.get(0).getSessionNumber(),historycinfos.get(0).getSessionNumber(), "验证getSessionNumber");
			Logger.verifyEquals(cinfos.get(0).getStatus(),historycinfos.get(0).getStatus(), "验证getStatus");
			Logger.verifyEquals(cinfos.get(0).getUserId(),historycinfos.get(0).getUserId(), "验证getUserId");
			Logger.verifyEquals(cinfos.get(0).getUserName(),historycinfos.get(0).getUserName(), "验证getUserName");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}